package com.lu.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.common.constants.ApUserExamineContants;
import com.lu.model.admin.dto.AuthDto;
import com.lu.model.common.dtos.PageResponseResult;
import com.lu.model.common.dtos.ResponseResult;
import com.lu.model.common.enums.AppHttpCodeEnum;
import com.lu.model.user.pojo.ApUserRealname;
import com.lu.user.mapper.ApUserRealnameMapper;
import com.lu.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    /**
     * 待审核用户列表查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listExamine(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ApUserRealname::getCreatedTime);
        Page<ApUserRealname> data = this.page(page, queryWrapper);

        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) data.getTotal());
        result.setData(data.getRecords());

        return result;
    }

    /**
     * 审核不通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult failExamine(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = this.getOne(Wrappers.<ApUserRealname>lambdaQuery().eq(ApUserRealname::getId, dto.getId()));
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUserRealname.setReason(dto.getMsg());
        apUserRealname.setStatus(ApUserExamineContants.EXAMINE_FAIL);
        apUserRealname.setUpdatedTime(new Date());
        this.updateById(apUserRealname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult successExamine(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = this.getOne(Wrappers.<ApUserRealname>lambdaQuery().eq(ApUserRealname::getId, dto.getId()));
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        apUserRealname.setStatus(ApUserExamineContants.EXAMINE_PASS);
        apUserRealname.setUpdatedTime(new Date());
        this.updateById(apUserRealname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
